package SerialTest1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.math.BigInteger;

import javax.swing.*;

public class ChatGUI {
	public static JTextArea chatSend_Hex;
	public static JTextArea chatSend_Decimal;
	public static JTextArea chatSend_Ascii;
	public static JTextArea chatReceive_Hex;
	public static JTextArea chatReceive_Decimal;
	public static JTextArea chatReceive_Ascii;
	public JScrollPane sp_send_Hex;
	public JScrollPane sp_send_Decimal;
	public JScrollPane sp_send_Ascii;
	public JScrollPane sp_receive_Hex;
	public JScrollPane sp_receive_Decimal;
	public JScrollPane sp_receive_Ascii;
	public JPanel panel;
	public JPanel mainPane;
	public JTabbedPane tabPanel1;
	public JTabbedPane tabPanel2;
	public JPanel UpPanel;
	public JPanel DownPanel;
	public JButton clearbtn_send;
	public JButton clearbtn_receive;
	DataInputStream in;
	
	public ChatGUI() {
		tabPanel1 = new JTabbedPane();
		tabPanel2 = new JTabbedPane();
		UpPanel = new JPanel(new BorderLayout());
		DownPanel = new JPanel(new BorderLayout());
		JLabel lb1 = new JLabel("Send Window");
		JLabel lb2 = new JLabel("Receive Window");
		lb1.setHorizontalAlignment(SwingConstants.CENTER);
		lb2.setHorizontalAlignment(SwingConstants.CENTER);
		
		tabPanel1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		tabPanel2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

		chatSend_Hex = new JTextArea(14, 35);
		chatSend_Hex.setEditable(false);
		chatSend_Hex.setForeground(Color.blue);
		chatSend_Hex.setLineWrap(true);
		sp_send_Hex = new JScrollPane(chatSend_Hex,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		chatReceive_Hex = new JTextArea(14, 35);
		chatReceive_Hex.setEditable(false);
		chatReceive_Hex.setLineWrap(true);
		chatReceive_Hex.setForeground(Color.green);
		sp_receive_Hex = new JScrollPane(chatReceive_Hex,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		tabPanel1.addTab("Hex", sp_send_Hex);
		tabPanel2.addTab("Hex", sp_receive_Hex);

		chatSend_Decimal = new JTextArea(14, 35);
		chatSend_Decimal.setEditable(false);
		chatSend_Decimal.setForeground(Color.blue);
		chatSend_Decimal.setLineWrap(true);
		sp_send_Decimal = new JScrollPane(chatSend_Decimal,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		chatReceive_Decimal = new JTextArea(14, 35);
		chatReceive_Decimal.setEditable(false);
		chatReceive_Decimal.setLineWrap(true);
		chatReceive_Decimal.setForeground(Color.green);
		sp_receive_Decimal = new JScrollPane(chatReceive_Decimal,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		tabPanel1.addTab("Decimal", sp_send_Decimal);
		tabPanel2.addTab("Decimal", sp_receive_Decimal);

		chatSend_Ascii = new JTextArea(14, 35);
		chatSend_Ascii.setEditable(false);
		chatSend_Ascii.setForeground(Color.blue);
		chatSend_Ascii.setLineWrap(true);
		sp_send_Ascii = new JScrollPane(chatSend_Ascii,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		chatReceive_Ascii = new JTextArea(14, 35);
		chatReceive_Ascii.setEditable(false);
		chatReceive_Ascii.setLineWrap(true);
		chatReceive_Ascii.setForeground(Color.green);
		sp_receive_Ascii = new JScrollPane(chatReceive_Ascii,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		tabPanel1.addTab("ASCII", sp_send_Ascii);
		tabPanel2.addTab("ASCII", sp_receive_Ascii);
		tabPanel1.setSelectedIndex(2);
		tabPanel2.setSelectedIndex(2);

		tabPanel1.setBackground(new Color(0,0,0,45));
		tabPanel2.setBackground(new Color(0,0,0,45));
		clearbtn_send = new JButton("Clear");
		clearbtn_send.setActionCommand("Clear");
		clearbtn_send.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// text area clear
				if (e.getActionCommand().equals("Clear")) {
					chatSend_Ascii.setText("");
					chatSend_Decimal.setText("");
					chatSend_Hex.setText("");
				}
			}
		});

		clearbtn_receive = new JButton("Clear");
		clearbtn_receive.setActionCommand("Clear");
		clearbtn_receive.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// text area clear
				if (e.getActionCommand().equals("Clear")) {
					chatReceive_Ascii.setText("");
					chatReceive_Decimal.setText("");
					chatReceive_Hex.setText("");
				}
			}
		});

		UpPanel.add(lb1, "North");
		UpPanel.add(tabPanel1, "Center");
		UpPanel.add(clearbtn_send, "South");
		DownPanel.add(lb2, "North");
		DownPanel.add(tabPanel2, "Center");
		DownPanel.add(clearbtn_receive, "South");
		panel = new JPanel(new BorderLayout());
		panel.add(UpPanel, "North");
		panel.add(DownPanel, "South");
	}

	public void sendData(byte[] buf) {
		chatSend_Ascii.append(byteToASCII(buf) + "\n");
		chatSend_Hex.append(byteToHex(buf) + "\n");
		chatSend_Decimal.append(byteToDecimal(buf) + "\n");
	}

	public static void receiveData(byte[] buf) {
		chatReceive_Ascii.append(byteToASCII(buf)+ "\n");
		chatReceive_Hex.append(byteToHex(buf) + "\n");
		chatReceive_Decimal.append(byteToDecimal(buf) + "\n");
	}

	public static void main(String[] args) {
		new ChatGUI();
	}

	public static String byteToHex(byte[] b) {
		String result = "";
		String s = new String(b);
		for (int i = 0; i < s.length(); i++) {
			result += String.format("0x%02X ", (int) s.charAt(i));
		}

		return result;
	}

	public static String byteToASCII(byte[] b) {
		String s = new String(b);
		return s;
	}

	public static String byteToDecimal(byte[] b) {
		String result = "";

		for (int i = 0; i < b.length; i++) {
			result += (int) b[i] + " ";
		}

		return result;
	}
}